import java.util.*;

class Solution {
    public static int longestConsecutive(int[] nums) {
        int len = nums.length;

        if (len == 0) {
            return 0;
        }

        //给 nums 去重
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        Integer[] arr = set.toArray(new Integer[set.size()]);

        Arrays.sort(arr);

        //双指针
        int left = 0, right = 1;
        int max = 0;
        int n = arr.length;
        int count = 1;
        while (right < n && left < right) {
            if (arr[right] == arr[left] + count) {
                right++;
                count++;
            } else {
                max = Math.max(right - left, max);
                left++;
                right = left + 1;
                count = 1;
            }
        }

        //判断 right 结束的情况
        max = Math.max(max, right - left);

        return max;
    }

    public static void main(String[] args) {
        longestConsecutive(new int[]{100,4,200,1,3,2});
    }
}